<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\FormException;
use App\Http\Controllers\LayuiController;
use App\Models\RechargeQuick;
use App\Services\UploadService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class RechargeQuickController extends Controller
{

    /**
     * 列表页
     * @name index
     * @param \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/21 9:42
     */
    public function index(){
        return view('Admin.rechargeQuick.index');
    }

    /**
     * 列表页表格数据方法
     * @name table
     * @param array
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/21 9:43
     */
    public function table(Request $request){

        $data = RechargeQuick::get()
            ->transform(function($item){
                return [
                    'id'    => $item->id,
                    'photo' => $item->photo,
                    'money' => number_format($item->money,2,".",""),
                    'status'=> $item->status
                ];
            });

        return LayuiController::tableJson($data,0,$data->count());
    }

    /**
     * 编辑展示页面
     * @name edit
     * @param \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/21 9:43
     */
    public function edit(Request $request,$id){
        $data = RechargeQuick::findOrFail($id);
        return view('Admin.rechargeQuick.edit',['data' => $data]);
    }

    /**
     * 编辑AJAX保存操作
     * @name update
     * @param void
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/21 10:11
     */
    public function update(Request $request,$id){
        $payload  = $request->only(['money']);
        $payload['status'] = $request->input('status',RechargeQuick::StatusFail);
        Validator::make($payload, [
            'money'        => 'required|numeric',
        ],[
            'money.required'     => '请输入充值金额',
            'money.numeric'      => '请输入充值金额',
        ])->validate();

        DB::beginTransaction();

        try{
            $model = RechargeQuick::findOrFail($id);

            if($request->hasFile('photo')){
                $UploadService = new UploadService($request,'rechargeQuick','photo','oss');
                $uploadresult = $UploadService->upload();
                if($uploadresult['status'] == UploadService::FailStatus){
                    throw new \Exception($uploadresult->message ?? '图片上传失败');
                }
                $model->photo = $uploadresult['url'];
            }

            $model->status = $payload['status'];
            $model->money = $payload['money'];

            $model->save();

        }catch (\Exception $exception){
            DB::rollBack();
            throw new FormException(App::environment('local') ? $exception->getMessage() : '保存失败' );
        }
        DB::commit();
        return $this->getResponse('保存成功');
    }

    /**
     * 编辑单个字段
     * @name updateField
     * @param \Illuminate\Http\JsonResponse
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/22 14:01
     */
    public function updateField(Request $request, $id)
    {
        DB::beginTransaction();

        try{
            $field = $request->input('field');
            $value = $request->input('value');

            if($field == 'money')
                $value = floatval($value);

            $model = RechargeQuick::findOrFail($id);
            $model->$field = $value;

            $model->save();

        }catch (\Exception $exception){

            DB::rollBack();
            throw new FormException(App::environment('local') ? $exception->getMessage() : '保存失败' );

        }
        DB::commit();
        return $this->getResponseSuccess('保存成功');
    }

    /**
     * 状态修改方法
     * @name status
     * @param \Illuminate\Http\JsonResponse
     * @author Mr.Liu
     * @return JsonStr
     * @datetime 2019/5/21 9:43
     */
    public function status(Request $request){

        DB::beginTransaction();
        try{
            $id = $request->input('id');
            $model = RechargeQuick::findOrFail($id);
            $model->status = $model->status == RechargeQuick::StatusSuccess ? RechargeQuick::StatusFail : RechargeQuick::StatusSuccess;
            $model->save();

        }catch (\Exception $exception){

            DB::rollBack();
            throw new FormException(App::environment('local') ? $exception->getMessage() : '设置失败' );

        }

        DB::commit();
        return $this->getResponseSuccess('设置成功');
    }
}
